package com.tbit.uqbike.client.controller;

import com.tbit.common.entity.UserCapitalChange;
import com.tbit.uqbike.client.constant.ResponseCode;
import com.tbit.uqbike.client.exception.BaseException;
import com.tbit.uqbike.client.pojo.User;
import com.tbit.uqbike.client.pojo.UserAccountLog;
import com.tbit.uqbike.client.service.TokenService;
import com.tbit.uqbike.client.service.UserAccountLogService;
import com.tbit.uqbike.client.service.UserCapitalChangeService;
import com.tbit.uqbike.client.util.OutputUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户资金变动日志
 *
 * @author Leon
 * 2017年2月24日 下午5:51:05
 */
@Controller
@RequestMapping("/userAccountLog")
public class UserAccountLogController {
    @Autowired
    private UserAccountLogService userAccountLogService;
    @Autowired
    private TokenService tokenService;

    @Autowired
    private UserCapitalChangeService userCapitalChangeService;


    /**
     * 获取用户资金变动记录-新版
     *
     * @param token
     * @param session
     * @param request
     * @param response
     */
    @RequestMapping("/getUserCapitalChange")
    public void getUserCapitalChange(String token, HttpSession session, HttpServletRequest request,
                                     HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();

        try {
            User user = tokenService.getUser(token);

            List<UserCapitalChange> userCapitalChanges = userCapitalChangeService.getUserCapitalChange(user.getUserId());

            map.put(ResponseCode.RET, ResponseCode.SUCC);
            map.put(ResponseCode.DATA, userCapitalChanges);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }

        OutputUtil.renderText(request, response, map);
    }


    /**
     * 获取用户资金变动记录
     *
     * @param token
     * @param session  会话
     * @param response 响应
     * @ignoreParams request
     */
    @RequestMapping("/getByUserId")
    public void getByUserId(String token, HttpSession session, HttpServletRequest request,
                            HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();

        try {
            User user = tokenService.getUser(token);

            List<UserAccountLog> userAccountLogs = userAccountLogService.getByUserId(user.getUserId());

            map.put(ResponseCode.RET, ResponseCode.SUCC);
            map.put(ResponseCode.DATA, userAccountLogs);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }

        OutputUtil.renderText(request, response, map);
    }


    /**
     * 分页查询用户资金变动记录* @param token
     *
     * @param rowCount 每页大小
     * @param pageNO   页码数
     * @param type     类型
     * @param session  会话
     * @param response 响应
     * @ignoreParams request
     */
    @RequestMapping("/queryPage")
    public void queryPage(String token, Integer rowCount, Integer pageNO, Integer type,
                          HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();

        try {
            User user = tokenService.getUser(token);

            /*默认值*/
            if (rowCount == null || rowCount < 1) {
                rowCount = 10;
            }
            if (pageNO == null || pageNO < 1) {
                pageNO = 1;
            }

            List<UserAccountLog> userAccountLogs = userAccountLogService.queryPage(null, user.getUserId(), type, rowCount, pageNO);

            /*获取总数*/
            Integer count = userAccountLogService.queryCount(user.getAccountId(), user.getUserId(), type);
            map.put("total", count);

            map.put(ResponseCode.RET, ResponseCode.SUCC);
            map.put(ResponseCode.DATA, userAccountLogs);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }

        OutputUtil.renderText(request, response, map);
    }

}